//
//  LoginVM.swift
//  NewXueBa
//
//  Created by Shine on 2019/6/3.
//  Copyright © 2019 jmxQAQ. All rights reserved.
//

import Foundation

class LoginVM {
    
    let disposeBag = DisposeBag()
    let dataModel = Variable(LoginModel())
    
    /// 登录请求
    ///
    /// - Parameters:
    ///   - phone: 号码
    ///   - pwd: MD5密码加密
    ///   - ver: 客户端版本号
    ///   - ctype: 客户端类型 
    ///   - resultBlock: 回调返回
    /*
     ClientType_PC      = 10 //pc端类型
     ClientType_Android = 30 //安卓端类型
     ClientType_Ios     = 50 //ios端类型
     ClientType_Mac     = 70 //mac端类型
     ClientType_Mac     = 90 //web端类型
     */
    func loginRequest(_ phone: String,
                      _ pwd: String,
                      _ ver: String,
                      _ ctype: String,
                      _ resultBlock: @escaping()->() ) {
        let token = BCAPI.studyLogin(phone: phone, pwd: pwd, ver: ver, ctype: ctype)
        ApiLoadingProvider.request(token, model: LoginModel.self) {
           [weak self] (returnData) in
            if let code = returnData?.code, code == 0 {
                self?.dataModel.value = returnData ?? LoginModel()
                self?.setUserData()
                resultBlock()
            }
        }
    }
    
    /// 获取验证码
    ///
    /// - Parameters:
    ///   - phone:手机号
    ///   - verify_code: md5sum(phone#phone[0:4]#ver#ctype) 大写的
    ///   - ver: 客户端版本号
    ///   - ctype: 客户端类型
    ///   - resultBlock:
     func getMsgMode( _ phone: String,
                      _ verify_code: String,
                      _ ver: String,
                      _ ctype: String,
                      _ resultBlock: @escaping( _ backCode: NSInteger)->() ) {
        let token = BCAPI.get_msg_code(phone: phone, verify_code: verify_code, ver: ver, ctype: ctype)
        ApiLoadingProvider.request(token, model: BaseModel.self) {
            (returnData) in
            if let code = returnData?.code {
                resultBlock(code)
            }
        }
    }
    
       /// 验证码登录
       ///
       /// - Parameters:
       ///   - phone: 手机号
       ///   - verify_code:
       ///   - code: 验证码
       ///   - ver:
       ///   - ctype:
       ///   - resultBlock:
       func codeLogin(_ phone: String,
                      _ verify_code: String,
                      _ code: String,
                      _ ver: String,
                      _ ctype: String,
                      _ resultBlock: @escaping(_ backCode: NSInteger)->() ) {
        let token = BCAPI.code_login(phone: phone, verify_code: verify_code, code: code, ver: ver, ctype: ctype)
        ApiLoadingProvider.request(token, model: LoginModel.self) {
            [weak self] (returnData) in
            if let code = returnData?.code, code == 0 {
                self?.dataModel.value = returnData ?? LoginModel()
                self?.setUserData()
            }
            resultBlock(returnData?.code ?? 0)
        }
    }
    // 保存个人信息数据
   fileprivate func setUserData() {
    BCUserManager.shareManager.user_name = self.dataModel.value.nick_name
    BCUserManager.shareManager.user_phone = self.dataModel.value.phoneno
    BCUserManager.shareManager.user_pwd = self.dataModel.value.pwd
    BCUserManager.shareManager.user_uid = self.dataModel.value.uid
    BCUserManager.shareManager.user_role = String(describing: self.dataModel.value.role)
    BCUserManager.shareManager.user_photo_url = self.dataModel.value.photo_url
    BCUserManager.shareManager.user_token = self.dataModel.value.token
    BCUserManager.shareManager.isLogin = true
    }
    
}


